package com.myres.log.db;


import com.myres.db.ConnectionPool;
import com.myres.db.ModelService;
import com.myres.model.Category;

import java.util.List;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.myres.model.Product;
import com.myres.model.User;
import com.myres.util.UIDTool;
public class LogService {
	
	public User selectUser(String field,String email,String password) {
		Connection con=null;
		try{
		con=ConnectionPool.getPool().getConnection();
		PreparedStatement pstmt=con.prepareStatement("select *from res_users where "+ field+" =? and _password=? ");
		pstmt.setString(1, email);
		pstmt.setString(2, password);
		ResultSet rs=pstmt.executeQuery();
		List<User> users=User.populate(rs);
		if(users.size()==0) {
			System.out.println("no such user");
			return null;
		}
		User user=users.get(0);
		     System.out.println("user.id="+user.getId());
		con.close();
		int score=new ModelService().getScore(user.getId());
		System.out.println("read score when log="+score);
		user.setScore(score);
		return user;
	}catch(SQLException e){
		e.printStackTrace();
		return null;
	}finally{
		try{
		con.close();
		}catch(Exception e){
			
		}
	}
	}
	
	public User selectUser(int uid){
		Connection con=null;
		try{
		con=ConnectionPool.getPool().getConnection();
		PreparedStatement pstmt=con.prepareStatement("select * from res_users where _id=?");
		pstmt.setInt(1, uid);
		ResultSet rs=pstmt.executeQuery();
		User user=User.populate(rs).get(0);
		int score=new ModelService().getScore(user.getId());
		System.out.println("read score when log="+score);
		user.setScore(score);
		return user;
	}catch(SQLException e){
		e.printStackTrace();
		return null;
	}finally{
		try{
		con.close();
		}catch(Exception e){
			
		}
	}
	}
   public int updateToke(String token,int userId){
	   Connection con=null;
		try{
		con=ConnectionPool.getPool().getConnection();
		PreparedStatement pstmt=con.prepareStatement("update res_users set _token = ? where _id =?");
		pstmt.setString(1, token);
		pstmt.setInt(2, userId);
		return pstmt.executeUpdate();
		
	}catch(SQLException e){
		e.printStackTrace();
		return -1;
	}finally{
		try{
		con.close();
		}catch(Exception e){
			
		}
	}
   }
   public User selectUser(String smsToken){
	   Connection con=null;
		try{
		con=ConnectionPool.getPool().getConnection();
		PreparedStatement pstmt=con.prepareStatement("select * from res_users where _sms_token=?");
		pstmt.setString(1, smsToken);
		ResultSet rs=pstmt.executeQuery();
		List<User> u=User.populate(rs);
		if(u.size()==0) return null;
		System.out.print(u.size());
		User user=u.get(0);
		int score=new ModelService().getScore(user.getId());
		System.out.println("read score when log="+score);
		user.setScore(score);
		return user;
	}catch(SQLException e){
		e.printStackTrace();
		return null;
	}finally{
		try{
		con.close();
		}catch(Exception e){
			
		}
	}
	}
}
